<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>积分管理</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
    }
    .container {
      width: 80%;
      margin: 50px auto;
      padding: 20px;
      background: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
    h2 {
      text-align: center;
      color: #333;
    }
    form {
      display: flex;
      flex-direction: column;
    }
    label {
      margin-bottom: 5px;
      color: #555;
    }
    input[type="text"],
    input[type="number"],
    textarea,
    input[type="submit"] {
      padding: 10px;
      margin-bottom: 15px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    input[type="submit"] {
      background-color: #28a745;
      color: white;
      border: none;
      cursor: pointer;
    }
    input[type="submit"]:hover {
      background-color: #218838;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    th, td {
      padding: 12px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }
    th {
      background-color: #f2f2f2;
    }
    tr:hover {
      background-color: #f5f5f5;
    }
  </style>
</head>
<body>
<div class="container">
  <h2>积分管理</h2>
  <button onclick="fetchPoints()">刷新积分列表</button>
  <form id="addPointsForm">
    <label for="residentId">居民ID:</label>
    <input type="number" id="residentId" name="residentId" required>

    <label for="points">积分:</label>
    <input type="number" id="points" name="points" required>

    <input type="submit" value="添加积分">
  </form>

  <table id="pointsTable">
    <thead>
    <tr>
      <th>积分ID</th>
      <th>居民ID</th>
      <th>积分</th>
      <th>最后更新时间</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <!-- 积分数据将在这里动态插入 -->
    </tbody>
  </table>
</div>

<script>
  function fetchPoints() {
    fetch('http://localhost:8081/waste/api/points/getAll')
            .then(response => response.json())
            .then(points => {
              const tableBody = document.querySelector('#pointsTable tbody');
              tableBody.innerHTML = ''; // 清空表格内容

              points.forEach(point => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${point.pointsId}</td>
                    <td>${point.residentId}</td>
                    <td>${point.points}</td>
                    <td>${new Date(point.lastUpdated).toLocaleString()}</td>
                    <td>
                        <button onclick="editPoints(${point.residentId})">编辑</button>
                        <button onclick="deletePoints(${point.residentId})">删除</button>
                    </td>
                `;
                tableBody.appendChild(row);
              });
            })
            .catch(error => {
              alert('获取积分数据失败，请检查网络连接！');
              console.error('Error:', error);
            });
  }
  // 添加积分记录
  document.getElementById('addPointsForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const formData = new FormData(this);
    const data = {
      residentId: formData.get('residentId'),
      points: formData.get('points')
    };

    fetch('http://localhost:8081/waste/api/points/add', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
            .then(response => response.json())
            .then(data => {
              alert('积分记录添加成功！');
            })
            .catch(error => {
              alert('添加失败，请检查网络连接！');
              console.error('Error:', error);
            });
    fetchPoints();
  });

  // 编辑积分记录
  function editPoints(residentId) {
    const points = prompt('请输入新的积分:');
    if (points === null || points.trim() === '') {
      alert('请输入有效的积分值！');
      return;
    }

    const data = {
      residentId: residentId,
      points: parseInt(points, 10)
    };

    fetch('http://localhost:8081/waste/api/points/update', {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
            .then(response => response.json())
            .then(responseData => {
              alert('积分记录修改成功！');
            })
            .catch(error => {
            });
    fetchPoints();
  }

  // 删除积分记录
  function deletePoints(residentId) {
    if (!confirm('确定要删除该积分记录吗？')) {
      return;
    }

    fetch(`http://localhost:8081/waste/api/points/delete/${residentId}`, {
      method: 'DELETE'
    })
            .then(response => response.json())
            .then(responseData => {
              alert('积分记录删除成功！');
            })
    fetchPoints();
  }

  // 页面加载时调用
  window.onload = function() {
    fetchPoints();
  };
</script>
</body>
</html>